#!/usr/bin/env python
# -*- coding: utf-8 -*-
#2016年02月24日 星期五 20时09分40秒

from peewee import *
import datetime
#database = MySQLDatabase('www', **{'user': "'root'"})

database = MySQLDatabase('wordpress', user='root', password='123456',charset='utf8')

class UnknownField(object):
    pass

class BaseModel(Model):
    class Meta:
        database = database
class WpPosts(BaseModel):
    '''
    存储文章信息，
    如文章标题、正文、摘要、作者、发布时间、访问密码、评论数、
    修改时间、文章地址（非静态化之前的，带？和数字ID）等；
    
    '''
    id = PrimaryKeyField(db_column='ID', primary_key=True)
    comment_count = BigIntegerField(default = 0)
    comment_status = CharField(max_length=20,default = 'open')
    guid = CharField(max_length=255,default = 'open')
    menu_order = IntegerField(default = 0)
    ping_status = CharField(max_length=20,default = 'open')
    pinged = TextField(default = '')
    post_author = BigIntegerField(default = 1)
    post_content = TextField()
    post_content_filtered = TextField(default = '')
    post_date = DateTimeField(default = datetime.datetime.now())
    post_date_gmt = DateTimeField(default = datetime.datetime.now())
    post_excerpt = TextField(default = '')
    post_mime_type = CharField(max_length=100,default = '')
    post_modified = DateTimeField(default = datetime.datetime.now())
    post_modified_gmt = DateTimeField(default = datetime.datetime.now())
    post_name = CharField(max_length=200)
    post_parent = BigIntegerField(default = 0)
    post_password = CharField(max_length=20,default = '')
    post_status = CharField(max_length=20,default = 'publish')
    post_title = TextField()
    post_type = CharField(max_length=20,default = 'post')
    to_ping = TextField(default = '')

    class Meta:
        db_table = 'wp_posts'
        
class WpTerms(BaseModel):
    '''
    文章和链接分类以及文章的tag分类可以在wp_terms表里找到.
    term_id：分类ID
    name：分类名
    slug：缩略名
    term_group：未知
    '''
    term_id = PrimaryKeyField(primary_key=True)
    name = CharField(max_length=200)
    slug = CharField(max_length=200)
    term_group = BigIntegerField(default = 0)
    
    class Meta:
        db_table = 'wp_terms'
        
    def __unicode__(self):
        return self.term_id, self.name, self.slug

class WpTermTaxonomy(BaseModel):
    '''
    这张表描述了wp_terms表中每个条目的分类系统 (分类,链接,或tag).
    存储分类和标签的描述信息、父子关系、所属包含的文章数等；

    term_taxonomy_id：分类方法ID
    term_id：
    taxonomy：分类方法(category/post_tag)
    description：未知
    parent：所属父分类方法ID
    count：文章数统计
    '''
    term_taxonomy = PrimaryKeyField(db_column='term_taxonomy_id', primary_key=True)
    #term_id = BigIntegerField()
    term = ForeignKeyField(WpTerms,db_column='term_id',related_name='term')
    taxonomy = CharField(max_length=32)
    description = TextField(default = '')
    #parent = BigIntegerField(default = 0)
    parent = ForeignKeyField(WpTerms,db_column='parent',related_name='parent',default = 0)
    count = BigIntegerField()
        
    class Meta:
        db_table = 'wp_term_taxonomy'

class WpTermRelationships(BaseModel):
    '''
    	与文章有关的分类、来自wp_terms表的tags以及这一关联存在于wp_term_relationships表里. 
        链接与各自分类的联系也存储于这张表中.
        object_id：对应文章ID/链接ID
        term_taxonomy_id：对应分类方法ID
        term_order：排序
        CREATE TABLE `wp_term_relationships` (
        `object_id` bigint(20) unsigned NOT NULL DEFAULT '0',
        `term_taxonomy_id` bigint(20) unsigned NOT NULL DEFAULT '0',
        `term_order` int(11) NOT NULL DEFAULT '0',
        PRIMARY KEY (`object_id`,`term_taxonomy_id`),
        KEY `term_taxonomy_id` (`term_taxonomy_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |

    '''
    #object = BigIntegerField(db_column='object_id', primary_key=True)#post_id
    #term_taxonomy = BigIntegerField(db_column='term_taxonomy_id')#WpTermTaxonomy.term_taxonomy
    object = ForeignKeyField(WpPosts,db_column='object_id')
    term_taxonomy = ForeignKeyField(WpTermTaxonomy,db_column='term_taxonomy_id')
    term_order = IntegerField(default = 0)
    
    class Meta:
        db_table = 'wp_term_relationships'

class WpCommentmeta(BaseModel):
    comment = BigIntegerField(db_column='comment_id')
    meta = BigIntegerField(db_column='meta_id', primary_key=True)
    meta_key = CharField(max_length=255, null=True)
    meta_value = TextField(null=True)

    class Meta:
        db_table = 'wp_commentmeta'

class WpComments(BaseModel):
    comment = BigIntegerField(db_column='comment_ID', primary_key=True)
    comment_agent = CharField(max_length=255)
    comment_approved = CharField(max_length=20)
    comment_author = TextField()
    comment_author_ip = CharField(db_column='comment_author_IP', max_length=100)
    comment_author_email = CharField(max_length=100)
    comment_author_url = CharField(max_length=200)
    comment_content = TextField()
    comment_date = DateTimeField()
    comment_date_gmt = DateTimeField()
    comment_karma = IntegerField()
    comment_parent = BigIntegerField()
    comment_post = BigIntegerField(db_column='comment_post_ID')
    comment_type = CharField(max_length=20)
    user = BigIntegerField(db_column='user_id')

    class Meta:
        db_table = 'wp_comments'

class WpLinks(BaseModel):
    link_description = CharField(max_length=255)
    link = BigIntegerField(db_column='link_id', primary_key=True)
    link_image = CharField(max_length=255)
    link_name = CharField(max_length=255)
    link_notes = TextField()
    link_owner = BigIntegerField()
    link_rating = IntegerField()
    link_rel = CharField(max_length=255)
    link_rss = CharField(max_length=255)
    link_target = CharField(max_length=25)
    link_updated = DateTimeField()
    link_url = CharField(max_length=255)
    link_visible = CharField(max_length=20)

    class Meta:
        db_table = 'wp_links'

class WpOptions(BaseModel):
    autoload = CharField(max_length=20)
    option = BigIntegerField(db_column='option_id', primary_key=True)
    option_name = CharField(max_length=64)
    option_value = TextField()

    class Meta:
        db_table = 'wp_options'

class WpPostmeta(BaseModel):
    meta = BigIntegerField(db_column='meta_id', primary_key=True)
    meta_key = CharField(max_length=255, null=True)
    meta_value = TextField(null=True)
    post = BigIntegerField(db_column='post_id')

    class Meta:
        db_table = 'wp_postmeta'

class WpUsermeta(BaseModel):
    meta_key = CharField(max_length=255, null=True)
    meta_value = TextField(null=True)
    umeta = BigIntegerField(db_column='umeta_id', primary_key=True)
    user = BigIntegerField(db_column='user_id')

    class Meta:
        db_table = 'wp_usermeta'

class WpUsers(BaseModel):
    id = BigIntegerField(db_column='ID', primary_key=True)
    display_name = CharField(max_length=250)
    user_activation_key = CharField(max_length=60)
    user_email = CharField(max_length=100)
    user_login = CharField(max_length=60)
    user_nicename = CharField(max_length=50)
    user_pass = CharField(max_length=64)
    user_registered = DateTimeField()
    user_status = IntegerField()
    user_url = CharField(max_length=100)

    class Meta:
        db_table = 'wp_users'